<?php

/**
 * Capital Direct Lending (http://www.philbsd.com/cdl)
 *
 * Copyright (C) 2008 Philippine Business Solution Developers <http://www.philbsd.com>
 * All rights reserved.
 *
 *
 * LICENSE
 *
 * This source file is subject to the GNU General Public License v3 license
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-3.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@philbsd.com so we can send you a copy immediately.
 *
 * @copyright  (c) 2008 Philippine Business Solution Developers. (http://www.philbsd.com)
 * @license    http://www.gnu.org/licenses/gpl-3.0.txt
 * @version    $Id: App.php 42 2008-03-12 10:05:35Z philbsd $
 */

class Philbsd_App
{
    protected static $_instance = null;

    public $dirApplication;
    public $dirLibrary;

    public $config;

    public static function getInstance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
            self::$_instance->_initialize();
        }

        return self::$_instance;
    }

    public function run()
    {
        $frontController = Zend_Controller_Front::getInstance();

        $frontController->throwExceptions(true)
        				->setControllerDirectory($this->dirApplication . DS . 'controllers')
						->registerPlugin(new Philbsd_Controller_Plugin_View_Layout())
						->returnResponse(true);

        try {
            $response = $frontController->dispatch();
            $response->sendResponse();
        } catch (Exception $e) {
            echo $e->getMessage();
        }
    }

    protected function _initialize()
    {
        error_reporting(E_ALL | E_STRICT);

        $this->dirApplication = dirname(dirname(dirname(__FILE__))) . DS . 'app';

        $this->dirLibrary = $this->dirApplication . DS . 'lib';

        set_include_path($this->dirLibrary . PS
                       . $this->dirApplication . DS . 'models' . PS
                       . get_include_path());

        require_once 'Zend/Loader.php';
        Zend_Loader::registerAutoload();

        $this->config = new Zend_Config_Xml(
								$this->dirApplication . DS . 'etc' . DS .
								'config.xml', 'default');
		Zend_Registry::set('config', $this->config);
		Zend_Registry::set('db', Zend_Db::factory($this->config->database));
		Zend_Registry::set('acl', new Zend_Acl());
    }
}
